﻿

#include <iostream>
#include "../include/Board.h"

using namespace std;

// 清屏
void Board::clear()
{
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < N; j++)
		{
			board[i][j] = '.';
		}
	}
	// for (int i = 0; i < 7; i++)
	// 	for (int j = i + 8; j < N; j++)
	// 		board[i][j] = 'P';

	// for (int i = 7; i < N; i++)
	// 	for (int j = 0; j < i - 7; j++)
	// 		board[i][j] = 'P';
	count = 0;
}

// 初始化
Board::Board()
{
	clear();
}

// 打印棋盘
//void Board::print()
//{
//	cout << "   ";
//	for (int i = 0; i < N; i++)
//	{
//		string s = i < 10 ? "  " : " ";
//		cout << i << s;
//	}
//	cout << endl;
//	for (int i = 0; i < N; i++)
//	{
//		string s = i < 10 ? "  " : " ";
//		cout << i << s;
//		for (int j = 0; j < N; j++)
//		{
//			cout << board[i][j] << "  ";
//		}
//		cout << i;
//		cout << endl
//			 << endl;
//	}
//	cout << "   ";
//	for (int i = 0; i < N; i++)
//	{
//		string s = i < 10 ? "  " : " ";
//		cout << i << s;
//	}
//	cout << endl;
//}
void Board::print()
{
	int len_=0;

	//前7行
	while(len_<7)
	{
		for (int i = 0; i < N-len_-1; i++)
			cout << " ";
		cout<<len_<<" ";
		for (int k = 0; k <= len_; k++)
			cout << board[len_ -k][k] << " ";
		cout <<len_ << endl;
		len_++;
	}
	//中间行
	len_ = 0;
	int x = 7;
	int y = 0;
	int len=7,flag=1;
	while (len_ < 8)
	{
		if (len_ + 7 < 10)len = 7;
		else len = 6;
		for (int i = 0; i < len; i++)
			cout << " ";
		if (flag == 1)
		{
			flag = 0;

			cout << len_ + 7<<" ";
			for (y = len_; y <= len_ + 7; y++)
				cout << board[x - y][y] << " ";
			cout << len_ + 7 << endl;
			if (len_ == 7)
				break;
		}
		else
		{
			flag = 1;
			cout << "   ";
			if (len_ + 7 >= 10)
				cout << " ";
			for (y = len_+1; y <= len_ + 7; y++)
				cout << board[x - y+1][y] << " ";
			cout << endl;
			x += 2; len_++;
		}
	}

	//后七行
	len_ = 7
		;
	x = 14+8;
	y = 8;
	while (len_ >=0)
	{
		for (int i = 0; i <= N - len_+1; i++)
			cout << " ";
		for (int k=y;k<=14;k++)
			cout << board[x-k][k] << " ";
		cout  << endl;
		len_--; y++; x++;
	}
}
// 检查下棋合法性
bool Board::checkEmpty(int x, int y)
{
	if ((x < 7 && y >= x + 8) || (x >= 7 && y < x - 7))
		return false;

	if (x >= 0 && x <= N - 1 && y >= 0 && y <= N - 1 && board[x][y] == '.')
	{
		return true;
	}
	return false;
}

// 下棋
void Board::setValue(int x, int y, char c)
{
	board[x][y] = c;
	if (c == '.')
	{
		count--;
	}
	else
	{
		count++;
	}
	return;
}

// 判断胜利
bool Board::win(int x, int y)
{
	return rowOfFive(x, y) || columnOfFive(x, y) || mainDiagnolOfFive(x, y);
}

// 行检查
bool Board::rowOfFive(int x, int y)
{

	int temp = 1;
	int i = 1;
	while (y - i >= 0 && board[x][y - i] == board[x][y])
	{
		temp++;
		i++;
	}
	i = 1;
	while (y + i <= N - 1 && board[x][y + i] == board[x][y])
	{
		temp++;
		i++;
	}
	return (temp >= M);
}
// 列检查
bool Board::columnOfFive(int x, int y)
{

	int temp = 1;
	int i = 1;
	while (x - i >= 0 && board[x - i][y] == board[x][y])
	{
		temp++;
		i++;
	}
	i = 1;
	while (x + i <= N - 1 && board[x + i][y] == board[x][y])
	{
		temp++;
		i++;
	}
	return (temp >= M);
}
// 从左上到右下
bool Board::mainDiagnolOfFive(int x, int y)
{

	int temp = 1;
	int i = 1;
	while (x - i >= 0 && y - i >= 0 && board[x - i][y - i] == board[x][y])
	{
		temp++;
		i++;
	}
	i = 1;
	while (x + i <= N - 1 && y + i <= N - 1 && board[x + i][y + i] == board[x][y])
	{
		temp++;
		i++;
	}
	return (temp >= M);
}
// 从右上到左下
/*
bool Board::reverseDiagnolOfFive(int x, int y){

	int temp = 1;
	int i = 1;
	while (x-i >= 0 && y + i <= N-1 && board[x-i][y+i] == board[x][y]){
		temp++;
		i++;
	}
	i = 1;
	while (x + i <= N-1 && y - i >= 0 && board[x+i][y-i] == board[x][y]){
		temp++;
		i++;
	}
	return (temp >= M);
}
*/
// 判断是否平局
bool Board::draw()
{
	if (count == N * N)
	{
		return true;
	}
	return false;
}

// 获取当前位置的棋子
char Board::getValue(int i, int j)
{
	return board[i][j];
}

// 返回放置的棋数
int Board::getCount()
{
	return count;
}

string Board::toString()
{
	string s = "";
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < N; j++)
		{
			s += getValue(i, j);
		}
	}
	return s;
}
